package util;

import java.sql.*;

public class JDBC {
    static String url = "jdbc:mysql://localhost:3306/itcast";
    static String rootname = "root";
    static String password = "296924";
    static Connection conn = null;
    static ResultSet rs = null;
    static PreparedStatement pstmt = null;

        /**
         * 数据库连接
         *
         * @throws SQLException
         */
        public static void dbconn() throws SQLException {
            try {
                Class.forName("com.mysql.cj.jdbc.Driver");
                conn = DriverManager.getConnection(url, rootname, password);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        /**
         * 增加数据库信息和修改信息和修改信息
         *
         * @param sql
         * @return
         */
        public static int addsql(String sql) {
            int i = 0;
            try {
                pstmt = conn.prepareStatement(sql);
                i = pstmt.executeUpdate(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return i;
        }

        /**
         * 查找数据库信息
         *
         * @param sql
         * @return
         */
        public static ResultSet selectsql(String sql) {
            try {
                pstmt = conn.prepareStatement(sql);
                rs = pstmt.executeQuery(sql);
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return rs;
        }

        /**
         * 关闭数据库，释放资源
         */
        public static void closeconn() {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

